{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2fe020da",
   "metadata": {},
   "source": [
    "# Pandas cheatsheet 速查表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a4ae10d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 引入pandas\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "239cc71c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Series 单个列， DataFrame 数据表格\n",
    "# numpy 数学  pandas 数据\n",
    "# dataframe的创建 - 第一种格式\n",
    "df1 = pd.DataFrame({\n",
    "    \"a\":[11,111,7],\n",
    "    \"b\":[42,11,44],\n",
    "    \"c\":[2,23,14]\n",
    "},\n",
    "index = [9,7,8] #索引的设置\n",
    ")\n",
    "# dataframe默认是数字索引，从0开始的下标，通过index可以自行设置自定义索引\n",
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef3a74ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "# dataframe的创建 - 第二种格式 - 放入数组格式\n",
    "df2 = pd.DataFrame(\n",
    "[[1,2,3],\n",
    " [4,5,6],\n",
    " [7,8,9]    \n",
    "],\n",
    "index = [1,2,3], # 进行索引的设置\n",
    "columns = ['a','b','c'] #进行标题的设置\n",
    ")\n",
    "df2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4584a057",
   "metadata": {},
   "source": [
    "## 数据的形状改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f287842b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df3 = pd.melt(df1) #数据拆分，把标题拆分到数据中\n",
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dde29687",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.concat([df1,df2],axis=1) #进行数据的纵向拼接,如果axis参数为1，则进行横向拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d64603fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "df3.pivot(columns = 'variable',values = 'value')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40a49d5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a0cc0cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.sort_values('a') #对传入的某个列的值进行排序，由低到高，整个行都会跟着排序\n",
    "df1.sort_values('a',ascending=False)#由高到低"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "297cabb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.rename(columns = {'c':'cat'}) #更改数据的列名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e30006a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.sort_index() #对索引进行排序，由低到高\n",
    "df1.sort_index(ascending=False) #由高到低"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "410637e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.reset_index() #把索引变成第一列，重新创建默认索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e069b972",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.drop(columns=['c']) #删除一列，[]中传入列名"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "419a75cc",
   "metadata": {},
   "source": [
    "## 获取数据\n",
    "\n",
    "### 获取行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2fc71ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1[df1.a>77] # 通过逻辑进行索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff37dcc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.drop_duplicates() #删除有重复的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c45a0e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.head(2) #获数据头部，里面的参数代表你想取几行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14a6497c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.tail(2) #获取数据尾部数据，里面参数代表取几行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15d64e18",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.sample(frac=0.5) # 随机选择50%的数据，0.1代表10%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a40e83c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.sample(n=1) #随机选择多少行，n=行数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad5fef07",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.iloc[0:2] #通过行的下标获取，左闭右开,0:2代表下标为0和1的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ada6ab1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.loc[9:7] #通过行的名字获取"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6983c48a",
   "metadata": {},
   "source": [
    "### 获取列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "010da846",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1['a'] #单个获取列的方式\n",
    "df1.a #使用.的方式直接获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4f3d877",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1[['a','b']] #获取多个列的方式，记得使用两个[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "233d4cd5",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.filter(regex='a') #通过正则表达式来获取列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bdff045f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.loc[:,'a':'c'] #通过loc来切分列，loc传入列名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31bc65b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.iloc[:,0:2]#通过iloc来切分列，iloc传入索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63e6f49c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.loc[df1['a']>77,['a','c']] #取a列中>77的行并且只取a和c列"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f187031",
   "metadata": {},
   "source": [
    "### Pandas中的逻辑语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b010cdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.a.isin(df1.a) #判断一个series是否存在于另一个series中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6af30490",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.isnull(df1) #判断是否是空值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "850b363e",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.notnull(df1) #判断是否不是空值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f44b59b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.any() #有任何值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17e51617",
   "metadata": {},
   "outputs": [],
   "source": [
    "df1.all() #是否填满值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57f376ee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
